/*   The MIT License
 *
 *   Carina Engine
 *   Copyright (c) 2009 2010 2011 2012 Zdravko Velinov
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

%skeleton "./lalr1-ce-ext.cc"
%require "2.4"
%define parser_class_name "Parser"
%define namespace "Carina::Config"

%code requires
{
#include "carina/common/global.hh"
#include "carina/common/logging.hh"
#include "carina/carina-config-driver.hh"
#include "carina/carina-config-ast.hh"
#include "carina/ast.hh"

#define YYSTYPE Carina::AST::UnmanagedNode

typedef Carina::AST::Value<int> IntegerType;
typedef Carina::AST::Value<unsigned> UnsignedType;
typedef Carina::AST::Value<float> FloatType;
typedef Carina::AST::Value<bool> BooleanType;
}

%name-prefix="cconf_"
%parse-param { Driver& driver }
%lex-param { Driver& driver }

%locations
%initial-action
{
    @$.begin.filename = @$.end.filename = &driver.m_FileName;
};

//%debug
%error-verbose

%token                  T_END                   0   "end of file"

%token <Identifier>      T_IDENTIFIER                "identifier"
%token <IntegerType>     T_INTEGER                   "integer"
%token <UnsignedType>	 T_UNSIGNED                  "unsigned integer"
%token <FloatType>		 T_FLOAT                     "float"
%token <BooleanType>	 T_BOOLEAN                   "boolean"
%token <StringLiteral>   T_STRING_LITERAL            "string literal"

%type <Entry>						config_entry
%type <List>						entry_list
%type <void>                        carina_config_file

//%printer { if($$) debug_stream() << $$->getValue(); } "identifier" "string literal"
//%printer { if($$) debug_stream() << $$->getName(); } "variable" "function" "type"

%destructor { $$.destroy(); } <*>

%start carina_config_file

%code provides
{
#define YY_DECL Carina::Config::Parser::token_type cconf_lex(YYSTYPE* yylval, \
															 Carina::Config::Parser::location_type* yylloc, \
															 Carina::Config::Driver& driver)
YY_DECL;

namespace Carina
{
namespace Config
{
typedef AST::Value<string> Identifier;
}
}
}

%%

carina_config_file
    : entry_list	                                        { driver.setASTRoot($1); $$ = Node(); }
    ;

entry_list
    : /* empty */                                           { $$ = NodeT<AST::List>(); }
    | config_entry entry_list			                    { $$ = CreateNodeTyped<AST::List>(@$, CE_AST_SEMICOLON_SEPARATED_LIST, $1, $2); }
    ;

config_entry
	: "identifier" '=' "integer" ';'						{ $$ = CreateNodeTyped<Entry>(@$, $1->getValue(), $3); }
	| "identifier" '=' "unsigned integer" ';'				{ $$ = CreateNodeTyped<Entry>(@$, $1->getValue(), $3); }
	| "identifier" '=' "float" ';'							{ $$ = CreateNodeTyped<Entry>(@$, $1->getValue(), $3); }
	| "identifier" '=' "string literal" ';'					{ $$ = CreateNodeTyped<Entry>(@$, $1->getValue(), $3); }
	| "identifier" '=' '{' entry_list '}' ';'				{ $$ = CreateNodeTyped<Entry>(@$, $1->getValue(), CreateNode<Block>(@$, $4)); }
	;
%%

namespace Carina
{
namespace Config
{
void Parser::error(const Parser::location_type& l, const std::string& m)
{
    driver.error(l, m);
}
}
}
